﻿using System;
using System.Text;

using SBPweb.Communication.Shared.Library.Contacts;
using SBPweb.Communication.Shared.Library.Conversations;
using SBPweb.Communication.Client.Library.Serialization;

using SBPweb.Communication.Client.Library.Xmpp.Contacts;

namespace SBPweb.Communication.Client.Library.Xmpp.Conversations
{

    public partial class Message : MessageBase
    {

        #region Constants

        private const string ATTRIBUTE_bodyTypeName = "bodyTypeName";

        #endregion

        #region Private fields

        private Contact from;
        private Contact to;

        private object body;

        #endregion

        #region Protected fields

        protected internal bool isOwn;

        #endregion

        #region Constructor logic

        public Message()
            : base()
        {
        }

        #endregion

        #region Property accessors

        public override object Body
        {
            get
            {
                if (body == null && message != null)
                {
                    if (message.HasAttribute(ATTRIBUTE_bodyTypeName))
                    {
                        Type type = Type.GetType(message.GetAttribute(ATTRIBUTE_bodyTypeName));
                        body = ObjectSerializer.Deserialize(message.Body, type);
                    }
                    else
                    {
                        body = message.Body;
                    }
                }
                return body;
            }
            set
            {
                body = value;
                if (message != null)
                {
                    Type type = typeof(Object);
                    if (value != null)
                    {
                        type = value.GetType();

                        if (!typeof(string).IsAssignableFrom(type))
                        {
                            message.SetAttribute(ATTRIBUTE_bodyTypeName, type.AssemblyQualifiedName);
                            message.Body = ObjectSerializer.Serialize(value);
                        }
                        else
                        {
                            message.Body = value.ToString();
                        }
                    }
                    else
                    {
                        message.Body = null;
                    }
                }
            }
        }

        public override bool IsMyOwn
        {
            get
            {
                return isOwn;
            }
        }

        #endregion

        #region Methods

        #endregion

    }

}